import createAxios from '../utils/axios'
import { Local } from "@/utils/storage.ts";


export interface LoginParams {
  email: string;
  code: string;
}

export interface RegisterParams {
  email: string;
  code: string;
  password: string;
  confirmPassword: string;
}

// 发送验证码
export function sendVerificationCode(email: string) {
  return createAxios({
    url: '/auth/send-code',
    method: 'post',
    data: { email }
  });
}

// 登录
export function login(data: LoginParams) {
  return createAxios({
    url: '/shop/login/password',
    method: 'post',
    data
  });
}

// 注册
export function register(data: RegisterParams) {
  return createAxios({
    url: '/shop/login/register',
    method: 'post',
    data
  });
}

// 获取用户信息
export function getUserInfo() {
  return createAxios({
    url: '/shop/user/info',
    method: 'get'
  });
}

// 退出登录
export function logout() {
  return createAxios({
    url: '/auth/logout',
    method: 'post'
  });
}


// 获取消息列表
export function getMessageList(data: any) {
  return createAxios({
    url: '/shop/user_msg/list',
    method: 'POST',
    data: {
      page: data.page || 1,
      page_size: data.page_size || 10
    }
  });
}


//获取邮箱验证验证码
export function getEmailVerificationCode(email: string) {
  return createAxios({
    url: '/api/email/sendEmailCode',
    method: 'post',
    data: { email }
  });
}


//验证邮箱
export function verifyEmail(email: string, code: string) {
  return createAxios({
    url: '/api/email/verifyEmail',
    method: 'post',
    data: { email, code }
  });
}


//编辑用户信息
export function editUserInfo(data: any) {
  return createAxios({
    url: '/shop/user/edit',
    method: 'post',
    data
  });
}


//添加地址
export function addAddress(data: any) {
  return createAxios({
    url: '/shop/userAddress/add',
    method: 'post',
    data
  });
}

// 编辑地址
export function editAddress(data: any) {
  return createAxios({
    url: '/shop/userAddress/edit',
    method: 'post',
    data
  });
}


// 获取地址列表
export function getAddressList(params: { page: number, pageSize: number }) {
  return createAxios({
    url: '/shop/user_address/index',
    method: 'get',
    params
  });
}



//地址详情
export const getDetailAddress = (id: number) => {
  return createAxios({
    url: `/shop/userAddress/detail?id=${id}`,
    method: 'GET',
  });
}


//设置默认地址
export const setDefaultAddress = (id: number) => {
  return createAxios({
    url: '/shop/user_address/setDefault',
    method: 'POST',
    data: { id },
  });
}


// 删除地址
export const deleteUserAddress = (id: number) => {
  return createAxios({
    url: '/shop/userAddress/delete',
    method: 'POST',
    data: { id },
  });
}


//订单列表
export function getOrderList(params: { page: number, pageSize: number }) {
  return createAxios({
    url: '/shop/order/list',
    method: 'get',
    params
  });
}


//订单详情
export function getOrderDetail(id: number) {
  return createAxios({
    url: `/shop/order/detail?id=${id}`,
    method: 'get'
  });
}


//删除订单
export const deleteOrder = (params: any) => {
  return createAxios({
    url: `/shop/order/delete`,
    method: 'POST',
    data: params,
  });
}


//查看物流
//id 订单id
export const getLogistics = (params: any) => {
  return createAxios({
    url: `shop/order/delivery`,
    method: 'GET',
    params,
  });
}


//确认收货
export const receive = (params: any) => {
  return createAxios({
    url: `/shop/order/receive`,
    method: 'POST',
    data: params,
  });
}


//添加评论
export const addComment = (params: any) => {
  return createAxios({
    url: `shop/order_evaluate/add`,
    method: 'POST',
    data: params,
  });
}


//申请售后
export const applyAfterSale = (params: any) => {
  return createAxios({
    url: `/shop/orderRefund/refund`,
    method: 'POST',
    data: params,
  });
}


//我的全部售后
export const getMyAfterSale = (params: any) => {
  return createAxios({
    url: `shop/orderRefund/list`,
    method: 'GET',
    params,
  });
}


//售后详情
export const getAfterSaleDetail = (params: any) => {
  return createAxios({
    url: `shop/orderRefund/detail`,
    method: 'GET',
    params,
  });
}


//获取 物流公司
export const getLogisticsCompany = () => {
  return createAxios({
    url: `shop/delivery/company`,
    method: 'GET',
  });
}


//提交售后物流信息
export const submitAfterSaleLogistics = (params: any) => {
  return createAxios({
    url: `shop/orderRefund/refundReturn`,
    method: 'POST',
    data: params,
  });
}


//我的购物车列表
export const getCartList = (params: any) => {
  return createAxios({
    url: '/shop/userCart',
    method: 'GET',
    params,
  });
}


// 编辑购物车数量
export const editCartNum = (params: any) => {
  return createAxios({
    url: '/shop/userCart/edit',
    method: 'POST',
    data: params,
  });
}


//删除购物车某一个商品
export const deleteCartItem = (params: any) => {
  return createAxios({
    url: 'shop/userCart/delete',
    method: 'POST',
    data: params,
  });
}


//清空购物车
export const clearCart = () => {
  return createAxios({
    url: 'shop/userCart/clear',
    method: 'POST',
  });
}

//购物车下单
export const cartOrder = (params: any) => {
  return createAxios({
    url: 'shop/order/preCart',
    method: 'GET',
    params,
  });
}



//我的收藏
export const getMyCollect = (params: any) => {
  return createAxios({
    url: '/shop/user_favorite/list',
    method: 'GET',
    params,
  });
}


//删除收藏
export const deleteCollect = (params: any) => {
  return createAxios({
    url: '/shop/user_favorite/delete',
    method: 'POST',
    data: params,
  });
}


//添加收藏
export const addCollect = (params: any) => {
  return createAxios({
    url: '/shop/user_favorite/add',
    method: 'POST',
    data: params,
  });
}


//获取用户积分
export const userPoints = () => {
  return createAxios({
    url: 'shop/user/UserPoints',
    method: 'GET',
  });
}

//获取用户积分明细列表
export const getUserPointsLogList = (params: { page: number, page_size: number, type?: string, startDate?: string, endDate?: string }) => {
  return createAxios({
    url: '/shop/user/UserPointsLogList',
    method: 'GET',
    params
  });
}



// 获取可兑换优惠券列表
export const getCouponList = (page: number, limit: number, is_score: number = 0) => {
  return createAxios({
      url: `/shop/coupon/center?page=${page}&limit=${limit}&is_score=${is_score}`,
      method: 'GET',
  });
}


//兑换优惠卷操作
export const exchangeCoupon = (params: any) => {
  return createAxios({
    url: '/shop/coupon/receive',
    method: 'POST',
    data: params,
  });
}


// 获取我的优惠券列表
export const getMyCoupons = (params: any) => {
  return createAxios({
    url: '/shop/userCoupon/list',
    method: 'GET',
    params,
  });
}


//我的消息列表
export const getMyMessageList = (params: any) => {
  return createAxios({
    url: '/shop/user_msg/list',
    method: 'POST',
    data: params,
  });
}


//获取未读消息数量
export const getUnreadMessageCount = () => {
  return createAxios({
    url: 'shop/user_msg/countUnread',
    method: 'POST',
  });
}


//标记消息为已读
export const unmarkMessageAsRead = (id: number) => {
  return createAxios({
    url: 'shop/userMsg/updateReadStatus',
    method: 'POST',
    data: { id }, // 只需传递消息ID
  });
}


//更改用户头像
export const changeUserAvatar = (params: any) => {
  return createAxios({
    url: '/shop/user/edit',
    method: 'POST',
    data: params,
  });
}

//忘记密码
export const getverify = (params: LoginParams) => {
  return createAxios({
      url: '/api/reset/password/verify',
      method: 'POST',
      data: params,
  }, 'API1', {loading: true});
}
//重置密码邮件发送
export const getSendEmailCode = (email: number) => {
  // 返回的数据格式可以和服务端约定
  return createAxios({
      url: `/api/reset/password/email/send?email=${email}`,
      method: 'GET',
  }, 'API1');
}




//注销账号邮箱验证码
export const signoutSendCode = (email: string) => {
  return createAxios({
      url: `/api/send/signout/mail`,
      method: 'POST',
      data: {
          email
      }
  }, 'API1');
}

// 注销账号
export const cancelUserAccount = (data: any) => {
  return createAxios({
      url: `/api/signout`,
      method: 'POST',
      data,
  }, 'API1');
}

// 刷新token
export const refreshToken = () => {
  return createAxios({
      url: `/shop/login/refresh`,
      method: 'POST',
      headers: {
        'ba-user-refresh': Local.get('refresh_token'),
      }
  }, 'API1');
}
